import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../../shared/blade/blade-container-base";
import { FormBuilder, Validators, FormControl } from "@angular/forms";
import { DatePipe } from "@angular/common";
import { Command } from "../../../../../shared/toolbar.service";
import { Observable } from "rxjs/Observable";

import { MenuShortcut } from "../../../../../entity/menu_shortcut.model";
import { MenuShortcutService } from "../../../../../service/menu_shortcut.service";
@Component({
  selector: 'app-menu-shortcut-edit',
  templateUrl: './menu-shortcut-edit.component.html',
  styleUrls: ['./menu-shortcut-edit.component.css']
})
export class MenuShortcutEditComponent extends BladeContainerBase<MenuShortcut>{

  constructor(
    private datePipe: DatePipe,
    private fb: FormBuilder,
    private menuShortcutServer: MenuShortcutService,
  ) {
    super(fb);
  }

  ngOnInit() {
    if (!this.isNew) {
      this.commands = [
        new Command("save", "保存", "fa fa-save", "setofkey:update", (blade) => this.exeSaveChange(blade), (blade) => this.canSaveChange(blade)),
        new Command("reset", "重置", "fa fa-save", "setofkey:reset", (blade) => this.exeReset(blade), (blade) => this.canReset(blade)),
      ]
    }
    super.ngOnInit();

  }

  //保存
  exeSaveChange(blade: any): void {
    this.saveChange();
  }
  canSaveChange(blade: any): boolean {
    return this.hasChange() && !this.hasError();
  }
  //重置
  exeReset(blade: any): void {
    return this.formGroup.reset(this.currentEntity);
  }
  canReset(blade: any): boolean {
    return this.hasChange();
  }

  //创建表单
  createForm() {
    this.formGroup = this.fromBuider.group({
      "menuName": [this.currentEntity.menuName, [
        Validators.required,
        // Validators.pattern("[\u4e00-\u9fa5]{1,20}"),
        Validators.maxLength(20),
      ],],
      "shortCutSeq": [this.currentEntity.shortCutSeq, [
        Validators.pattern("[0-9]{1,3}"),
        Validators.maxLength(3),
      ],],
      // (control: FormControl): any => this.shortcutSeqAsnyValidator(control)
    });
  }
  //设置错误对象
  setFormError(): void {
    this.formErrors = {
      'menuName': '',
      'shortCutSeq': '',
    };
  }

  //设置表单验证信息
  setValidationMessages(): void {
    this.validationMessages = {
      'menuName': {
        'required': '不能为空。',
        'pattern': '由汉字组成。',
        'maxlength': '不能多于20个字符长度。',
      },
      'shortCutSeq': {
        'pattern': '由整数组成。',
        'maxlength': '不能多于3个字符长度。',
        'exist': '已存在。',
      },
    };
  }

  //异步校验排序是否存在
  // shortCutSeqAsnyValidator(control: FormControl): any {
  //   if (!control.value || control.value == this.currentEntity.shortCutSeq) {
  //     return Observable.of(null);
  //   }
  //   return this.menuShortcutServer.shortCutSeq(control.value, this.currentEntity.shortCutSeq)
  //     .map(res => res ? { exist: true } : null);
  // }

  //新增
  onCreate() {
    if (!this.hasError()) {
      this.bladeData.isLoading = true;
      let formValue = this.formGroup.value;
      this.currentEntity.menuName = formValue.menuName;
      this.currentEntity.shortCutSeq = formValue.shortCutSeq;

      this.menuShortcutServer.create(this.currentEntity)
        .then(res => {
          if (res == 200) {
            let parentContainerInstance = this.parentBlade.bladeContainerInstance;
            parentContainerInstance.refresh(res);
            this.bladeService.closeBlade(this.currentBlade);
          }
        })
        .catch(error => this.bladeData.error = error)
        .then(res => this.bladeData.isLoading = false);
    }
  }

  //更新
  saveChange(): any | Promise<any> | Observable<any> {
    if (this.canSaveChange(this.currentBlade)) {
      this.bladeData.isLoading = true;
      let formValue = this.formGroup.value;
      this.currentEntity.menuName = formValue.menuName;
      this.currentEntity.shortCutSeq = formValue.shortCutSeq;
      return this.menuShortcutServer.update(this.currentEntity)
        .then(res => {
          if (res) {
            (this.parentBlade.bladeContainerInstance).refresh(res);
            this.bladeService.closeBlade(this.currentBlade);
            return true;
          }
        })
        .catch(error => this.bladeData.error = error)
        .then(res => this.bladeData.isLoading = false);
    }
  }






}



// WEBPACK FOOTER //
// ./src/app/module/admin/role/menu-shortcut/menu-shortcut-edit/menu-shortcut-edit.component.ts
